/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fasea.index;

import fasea.model.IndexingFolder;
import fasea.model.IndexingFolderList;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.apache.lucene.index.IndexWriter;

/**
 *
 * @author lars-wolfram
 */
public class IndexerThreadExcecutor {

    private IndexerThread indexerThread;
    private ExecutorService executor = Executors.newFixedThreadPool(1); 

    public IndexerThreadExcecutor() {
        indexerThread = new IndexerThread();
        indexerThread.setPriority(Thread.MIN_PRIORITY);
    }
    
    public void setIndexerStatusListener(IndexerStatusListener listener){
        indexerThread.setIndexerStatusListener(listener);
    }

    public synchronized void startIndexing(final File indexFolder, final IndexingFolderList indexingFolders) throws IOException {
        try {
            if (!indexerThread.isRunning()) {
                indexerThread.setRunning(true);
                indexerThread.setIndexFolder(indexFolder);
                indexerThread.setIndexingFolders(indexingFolders);
                executor.execute(indexerThread);
            }
        }catch(RuntimeException ex){
            throw new IOException(ex.getCause().getMessage());
        }
    }

    public synchronized void abortIndexing() {
        if (indexerThread.isRunning()) {
            indexerThread.abortIndexing();
        }
    }
    
    public boolean isRunning(){
        return indexerThread.isRunning();
    }

    public IndexWriter getIndexWriter() {
        return indexerThread.getIndexWriter();
    }

    private class IndexerThread extends Thread {

        private volatile boolean isRunning = false;
        private Indexer indexer;
        private File indexFolder; 
        private IndexingFolderList indexingFolders;
        private IndexerStatusListener listener;
        
        @Override
        public void run() {
            try {
                indexer = new Indexer(indexFolder,indexingFolders);
                indexer.setStatusListener(listener);
                indexer.setDelay(0);
                indexer.startIndexing();
                indexer.optimize();
                indexer.close();
                isRunning = false;
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
        }

        public void abortIndexing() {
            if (indexer != null) {
                indexer.abort();
            }
        }

        public boolean isRunning() {
            return isRunning;
        }

        public void setIndexFolder(final File indexFolder) {
            this.indexFolder = indexFolder;
        }
        
        public void setIndexingFolders(final IndexingFolderList indexingFolders) {
            this.indexingFolders = indexingFolders;
        }

        private void setIndexerStatusListener(IndexerStatusListener listener) {
            if(indexer != null){
                this.indexer.setStatusListener(listener);
            }
            this.listener = listener;
        }

        private void setRunning(boolean b) {
            isRunning = b;
        }

        private IndexWriter getIndexWriter() {
            return indexer.getIndexWriter();
        }
    }
}
